package com.march.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.march.common.R;
import com.march.entry.Category;
import com.march.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/*
 * 分类管理
 * */

@Slf4j
@RestController
@RequestMapping("/categorys")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /*
     *新增菜品分类
     */
    @PostMapping
    public R<String> categoryAdd(@RequestBody Category category){

        categoryService.save(category);
        return R.success("添加成功");
    }

    /*
    *菜品分类分页
    * */
    @PostMapping("/page")
    public R<Page> page(int page,int pageSize){

        Page<Category> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();

        lqw.orderByAsc
                (Category::getSort);

        categoryService.page(pageInfo,lqw);

        return R.success(pageInfo);
    }
    /*
    * 菜品分类删除
    * */
    @GetMapping("/{id}")
    public R<String> remove(@PathVariable Long id){

//        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(Category::getId,id);
//        categoryService.removeById(id);
        categoryService.remove(id);
        return R.success("删除成功");
    }

    /*
    * 菜品分类修改
    * */
    @PutMapping
    public R<String> update(@RequestBody Category category, HttpServletRequest request){

        categoryService.updateById(category);

        return R.success("修改成功");
    }

    /*
    * 获取分类列表
    * */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){

        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(category.getType()!=null,Category::getType,category.getType());
        //分类列表
        List<Category> categoryList = categoryService.list(lqw);
        return R.success(categoryList);
    }


}
